package com.swss.g.sys.dao.impl;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import com.swss.g.base.impl.BaseDAOImpl;
import com.swss.g.sys.bean.GFunc;
import com.swss.g.sys.dao.GFuncDAO;


public class GFuncDAOImpl extends BaseDAOImpl<GFunc> implements GFuncDAO {

	public GFunc findFuncByName(String funcName) throws Exception {
		// TODO Auto-generated method stub
		try{
			final String hql= "FROM GFunc WHERE funcName='"+funcName.trim()+"'";
			return this.findOneByHql(hql);
			
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}

	public Boolean updateFuncs(final List<GFunc> funcList) throws Exception {
		// TODO Auto-generated method stub
		Boolean flag = (Boolean) getHibernateTemplate().execute(
				new HibernateCallback() {
					public Object doInHibernate(Session session)
							throws HibernateException, SQLException {
						try {
							for (GFunc f : funcList) {

								GFunc func = (GFunc) session.load(GFunc.class, f
										.getId());
								if (f.getFuncFlag() != null
										&& !f.getFuncFlag().equals("")) {
									func.setFuncFlag(f.getFuncFlag());
								}

								session.update(func);
								session.flush();
								session.clear();
							}
							return true;
						} catch (Exception ex) {
							ex.printStackTrace();
							return false;
						}
					}
				});
		return flag;
	}
	
}
